<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>密文列表</span>
      <el-button type="primary" style="float: right" @click="open" round
        >加密明文</el-button
      >
    </div>
    <div class="text item">
      <el-table :Loading="listLoading" :data="list" style="width: 100%">
        <el-table-column type="index" label="序号" width="100" />
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="ciphertext" label="密文">
          <template slot-scope="scope">
            <el-popover
              placement="top-start"
              title="密文"
              width="400"
              trigger="hover"
              :content="scope.row.ciphertext"
            >
              <el-button slot="reference">查看密文</el-button>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作">
          <template slot-scope="scope">
            <el-button type="primary" @click="opendialog(scope.row)"
              >解密</el-button
            >
            <el-button @click="delitem(scope.row.id)" type="danger"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      background
      style="margin: 20px 0px;float: right;"
      @current-change="handleCurrentChange"
      :page-size="size"
      layout="total,prev, pager, next"
      :total="total"
    >
    </el-pagination>
    <encrypt-form :show.sync="show" :fetchData="fetchData"></encrypt-form>

    <el-dialog title="输入私钥信息" :visible.sync="dialogprivate">
      <el-form :model="privateform" ref="privateform">
        <el-input type="hidden" v-model="privateform.public_key_id"></el-input>
        <el-input type="hidden" v-model="privateform.ciphertext"></el-input>
        <el-form-item label="私钥" label-width="80px">
          <el-input
            type="textarea"
            rows="4"
            v-model="privateform.private_key"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogprivate = false">取 消</el-button>
        <el-button type="primary" @click="decrypt">确 定</el-button>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
import { get, edit, del } from "@/api/encrypt";
import EncryptForm from "./Form.vue";
export default {
  name: "encrypt",
  components: {
    EncryptForm
  },
  props: {
    show: {
      default: false
    }
  },
  data() {
    return {
      show: false,
      list: [],
      page: 1,
      size: 10,
      total: null,
      listLoading: false,
      dialogprivate: false,
      privateform: {
        private_key: "",
        public_key_id: "",
        ciphertext: ""
      }
    };
  },
  methods: {
    open() {
      this.show = true;
    },
    fetchData() {
      this.listLoading = true;
      get({ page: this.page, size: this.size }).then(res => {
        this.list = res.data;
        this.listLoading = false;
      });
    },
    opendialog(data) {
      this.dialogprivate = true;
      this.privateform.ciphertext = data.ciphertext;
      this.privateform.public_key_id = data.public_key_id;
    },
    decrypt() {
      edit(this.privateform).then(res => {
        if (res.code === 200) {
          this.$alert(res.data, "明文内容");
        } else {
          this.$message.error(res.message);
        }
      });
    },
    delitem(data) {
      del(data).then(res => {
        if (res.code === 200) {
          this.$message.success(res.message);
          this.fetchData();
        } else {
          this.$message.error(res.message);
        }
      });
    }
  },
  mounted() {
    this.fetchData();
  }
};
</script>
